package net.lukecloud.xile.laundry;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.lukecloud.xile.App;
import net.lukecloud.xile.R;
import net.lukecloud.xile.activity.DeviceActivity;
import net.lukecloud.xile.model.Device;
import net.lukecloud.xile.net.ApiService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LaundryFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener ,MyDeviceRecyclerViewAdapter.OnListFragmentInteractionListener {

    private static final String ARG_COLUMN_COUNT = "column-count";
    private int mColumnCount = 1;
    public LaundryFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static LaundryFragment newInstance(int columnCount) {
        LaundryFragment fragment = new LaundryFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }
        apiService = ((App)getActivity(). getApplication()).retrofit.create(ApiService.class);
    }
    ApiService  apiService;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.refresh_list, container, false);

        mSwipeRefreshLayout= ((SwipeRefreshLayout)view);
        mSwipeRefreshLayout.setOnRefreshListener(this);
//        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        // Set the adapter
            Context context = view.getContext();
            RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
            if (mColumnCount <= 1) {
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
            } else {
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
            }
        mMyDeviceRecyclerViewAdapter =new MyDeviceRecyclerViewAdapter( this);
        recyclerView.setAdapter(mMyDeviceRecyclerViewAdapter);
        onRefresh();
        return view;
    }

    MyDeviceRecyclerViewAdapter mMyDeviceRecyclerViewAdapter;
    SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    public void onRefresh() {

         apiService.getDevices().enqueue(new Callback<List<Device>>() {
             @Override
             public void onResponse(Call<List<Device>> call, Response<List<Device>> response) {
                 mMyDeviceRecyclerViewAdapter.getValues().clear();
                mMyDeviceRecyclerViewAdapter.getValues().addAll(response.body());
                mMyDeviceRecyclerViewAdapter.notifyDataSetChanged();
                mSwipeRefreshLayout.setRefreshing(false);

             }

             @Override
             public void onFailure(Call<List<Device>> call, Throwable t) {
                 mSwipeRefreshLayout.setRefreshing(false);
             }
         });
    }


//    @Override
//    public void onAttach(Context context) {
//        super.onAttach(context);
//        if (context instanceof OnListFragmentInteractionListener) {
//            mListener = (OnListFragmentInteractionListener) context;
//        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnListFragmentInteractionListener");
//        }
//    }
//
//    @Override
//    public void onDetach() {
//        super.onDetach();
//        mListener = null;
//    }
//    OnListFragmentInteractionListener mListener;

    @Override
    public void onListFragmentInteraction(Device item) {
        startActivity(new Intent(getContext(), DeviceActivity.class));
    }

}
